< previous page page_183 next page >

Page 183
case, it is definitely not necessary because there is a better approach that is easier to understand and support. The following declaration is used in which all of the nonstring parameters are declared ByVal As Long:
Private Declare Function RegQueryInfoKeyV3 Lib "advapi32.dll" _
Alias "RegQueryInfoKeyA" (ByVal hKey As Long, ByVal lpClass As String, _
ByVal lpcbClass As Long, ByVal lpReserved As Long, ByVal lpcSubKeys As _
Long, ByVal lpcbMaxSubKeyLen As Long, ByVal lpcbMaxClassLen As Long, _
ByVal lpcValues As Long, ByVal lpcbMaxValueNameLen As Long, _
ByVal lpcbMaxValueLen As Long, ByVal lpcbSecurityDescriptor As Long, _
ByVal lpftLastWriteTime As Long) As Long
The trick here is (1) to explicitly pass a pointer to those variables that you wish to load with information and (2) to pass 0 for the remaining parameters. The VarPtr2 operator is used to obtain the address to the variables, and yes, it even works on stack parameters as shown here. And, as you can see, the function can now be implemented in a single line of code:
Private Function GetKeyInfo3(ByVal hKey As Long, NumberOfKeys _
As Long, MaxKeyNameLength As Long) As Long

   GetKeyInfo3 = RegQueryInfoKeyV3(hKey, vbNullString, 0, 0,
VarPtr(NumberOfKeys), VarPtr(MaxKeyNameLength), 0, 0, 0, 0, 0, 0)
End Function
Design Notes
Even though most of the examples in this book are small, throwaway code, that's no reason to ignore software design completely. If you look closely at the Reg2 sample program (on the CD that comes with this book), you'll see some additional code that suggests where the design is going. The CD also contains a class module called clsKeyValues that contains the following code:
' Class clsKeyValues
' Keys and values

Option Explicit
7017a6ead0e3c4111b47a554df321e9f.gif 7017a6ead0e3c4111b47a554df321e9f.gif
2 'The VarPtr operator is an undocumented operator. Normally, I would never advocate use of an undocumented function, but this one has been used so extensively in Microsoft's own code and samples that I think it is reasonably safe to use. The agGetAddressForObject function in the apigid32.dll included on the CD that comes with this book provides an alternate method for obtaining the address of a Visual Basic variable.

 
< previous page page_183 next page >